<template>
    <div class="heading-book">
        <!--    图书搜索    -->
        <el-row class="heading-book-search-and-new" >
                <el-input placeholder="请输入内容" v-model="input" class="input-with-select">
                    <el-select v-model="select" slot="prepend" placeholder="请选择">
                        <el-option label="IBSN" value="book_ibsn_id"></el-option>
                        <el-option label="书名" value="book_title"></el-option>
                        <el-option label="作者" value="book_author"></el-option>
                        <el-option label="出版社" value="book_publisher"></el-option>
                    </el-select>
                    <el-button slot="append" icon="el-icon-search" @click="searchBook"></el-button>
                </el-input>
        </el-row>
        <!--    图书信息列表  -->
        <el-table :data="booksData" style="width: 100%;margin-top: 10px">
            <el-table-column type="expand">
                <template slot-scope="props">
                    <el-form label-position="left" inline class="demo-table-expand">
                        <el-form-item label="图书封面">
                            <span>
                            <img :src="props.row.book_img" class="book-img">
                            </span>
                        </el-form-item>
                        <el-form-item label="图书名称">
                            <span>{{ props.row.book_title }}</span>
                        </el-form-item>
                        <el-form-item label="图书作者">
                            <span>{{ props.row.book_author }}</span>
                        </el-form-item>
                        <el-form-item label="图书出版社">
                            <span>{{ props.row.book_publisher }}</span>
                        </el-form-item>
                        <el-form-item label="图书出版时间">
                            <span>{{ props.row.book_pubdate }}</span>
                        </el-form-item>
                        <el-form-item label="图书页数">
                            <span>{{ props.row.book_page }}</span>
                        </el-form-item>
                        <el-form-item label="图书单价">
                            <span>{{ props.row.book_price }}</span>
                        </el-form-item>
                        <el-form-item label="图书库存数量">
                            <span>{{ props.row.book_now_number }}</span>
                        </el-form-item>
                        <el-form-item label="图书总数">
                            <span>{{ props.row.book_number }}</span>
                        </el-form-item>
                        <el-form-item label="图书库存地址">
                            <span>{{ props.row.book_address }}</span>
                        </el-form-item>
                        <el-form-item label="图书简介" style="width: 100%">
                            <span>{{ props.row.book_gist }}</span>
                        </el-form-item>
                    </el-form>
                </template>
            </el-table-column>
            <el-table-column
                    label="图书IBSN"
                    prop="book_ibsn_id">
            </el-table-column>
            <el-table-column
                    label="图书名称"
                    prop="book_title">
            </el-table-column>
            <el-table-column
                    label="图书作者"
                    prop="book_author">
            </el-table-column>
            <el-table-column
                    label="图书出版社"
                    prop="book_publisher">
            </el-table-column>
            <el-table-column label="操作">
                <template slot-scope="props">
                    <el-button type="primary" @click="borrowBook(props.$index, props.row)">借阅</el-button>
                </template>
            </el-table-column>
        </el-table>
        <!--    图书列表切换  -->
        <el-pagination
                @size-change="handleSizeChange"
                @current-change="handleCurrentChange"
                :current-page="defaultPage"
                :page-sizes="pages"
                :page-size="pages[pagesIndex]"
                layout="total, sizes, prev, pager, next, jumper"
                :total="totalNumber">
        </el-pagination>
    </div>
</template>

<script>
    export default {
        name: "StudentBooks",
        data(){
            return {
                // 查询图书
                input:'',
                select:'',
                // 图书列表
                booksData: [],
                // 图书列表分页显示
                defaultPage: 1,
                totalNumber:5,
                pages:[5,10,15,20],
                pagesIndex: 1,
            };
        },
        mounted() {
            this.getCard()
            this.getBooksList()
        },
        methods:{
            // 加载借书证信息
            getCard(){
                console.log('发送获取借阅证信息请求')
                this.$axios.get('/cards/search',{
                    params:{
                        role:this.$store.state.user.user_role,
                        type:'student_account',
                        input:this.$store.state.user.user_id
                    }
                }).then(res => {
                    if(res.data.code === 200){
                        console.log(res.data.msg)
                        const {library_card_id,student_name,student_department,student_major, student_phone, student_borrowed_number,student_limit,student_account,student_is_arrears,student_create_time} = res.data.msg[0]
                        this.$store.commit({
                            type:'card',
                            library_card_id:library_card_id,
                            student_name:student_name,
                            student_department:student_department,
                            student_major:student_major,
                            student_phone:student_phone,
                            student_borrowed_number:student_borrowed_number,
                            student_limit:student_limit,
                            student_account:student_account,
                            student_is_arrears:student_is_arrears,
                            student_create_time:student_create_time
                        })
                    }else{
                        this.$message({
                            message: '你没有借阅证?!请去办理...',
                            type: 'warning'
                        })
                        this.$store.commit('logout')
                        this.$router.replace({path:'/'})
                    }
                })
            },
            // 页面加载时获取数据库中图书列表
            getBooksList(){
                console.log('发送书籍获取请求！')
                this.$axios.get('/books/list',{
                    params:{
                        role:this.$store.state.user.user_role
                    }
                }).then(res=>{
                    if(res.data.code === 200){
                        this.booksData = res.data.msg
                        this.totalNumber = res.data.msg.length
                        // console.log(this.booksData);
                    }else{
                        this.$message({
                            message: '抱歉,服务器开小差了,请刷新重试~~~',
                            type: 'warning'
                        })
                    }
                })
            },
            // 搜索图书
            searchBook(){
                this.$axios.get('/books/search',{
                    params: {
                        type:this.select,
                        input:this.input
                    }
                }).then(res=>{
                    if(res.data.code === 200){
                        this.$message({
                            message: '查询图书信息成功！',
                            type: 'success'
                        })
                    }else{
                        this.$message({
                            message: '请确认图书信息完整性后重试！',
                            type: 'warning'
                        })
                    }
                    this.booksData = res.data.msg
                    // console.log(this.booksData)
                })
            },
            // 借阅图书
            borrowBook(index,row){
                let flag = true
                if(row.book_now_number === 0){
                    flag = false
                    this.$message({
                        message: '当前图书库存数量不足~~~',
                        type: 'warning'
                    })
                }
                if(flag && this.$store.state.card.student_is_arrears){
                    flag = false
                    this.$message({
                        message: '您存在欠缴账单,请缴费后再来借阅~~~',
                        type: 'warning'
                    })
                }
                if(flag && this.$store.state.card.student_borrowed_number === this.$store.state.card.student_limit){
                    flag = false
                    this.$message({
                        message: '啊,你快把书借完了,先还几本,再来借阅吧~~~',
                        type: 'warning'
                    })
                }
                if(flag){
                    this.$axios.get('/records/search',{
                        params:{
                            role:this.$store.state.user.user_role,
                            type:'record_book_ibsn',
                            input:row.book_ibsn_id,
                            id:this.$store.state.card.library_card_id
                        }
                    }).then(res=>{
                        const result = res.data.msg
                        for(let i=0;i<result.length;i++){
                            if(result[i].record_return > 0){
                                flag = false
                            }
                        }
                        if(!flag){
                            this.$message({
                                message: '啊,你已经借了这本书呀~~~',
                                type: 'warning'
                            })
                        }
                    })
                }
                if(flag){
                    let record = true
                    // 新建借阅记录
                    this.$axios.post('/records/new',{
                        role:this.$store.state.user.user_role,
                        row:row,
                        card:this.$store.state.card
                    }).then(res => {
                        if(res.data.code !== 200){
                            record = false
                        }
                    })
                    if(record){
                        // 修改图书信息
                        let number = row.book_now_number - 1
                        this.$axios.post('/books/update',{
                            role:this.$store.state.user.user_role,
                            ibsn:row.book_ibsn_id,
                            type:'book_now_number',
                            input:number
                        }).then(res => {
                            if(res.data.code ===200){
                                this.$message({
                                    message: '借阅成功~~~',
                                    type: 'success'
                                })
                            }
                        })
                    }
                }
            },
            // 切换每页显示的数量
            handleSizeChange(val) {
                console.log(`每页 ${val} 条`);
            },
            // 切换页面
            handleCurrentChange(val) {
                console.log(`当前页: ${val}`);
            }
        }
    }
</script>

<style scoped>
    .heading-book{
        margin: 10px;
    }
    .demo-table-expand {
        font-size: 0;
    }
    .demo-table-expand label {
        width: 90px;
        color: #99a9bf;
    }
    .demo-table-expand .el-form-item {
        margin-right: 0;
        margin-bottom: 0;
        width: 50%;
    }
    .book-img{
        height: 100px;
    }
</style>
